package com.jack.leetcode.array;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/***
 * @author zhangjie
 * @date 2020/3/22 20:20
 */
public class 使数组唯一的最小增量 {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        while(s.hasNext()){
            int n = s.nextInt();
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = s.nextInt();
            }
            System.out.println(minIncrementForUnique(a));
        }
    }

    public static int minIncrementForUnique(int[] A) {
        int result = 0;
        Arrays.sort(A);
        for (int i = 1; i < A.length; i++) {
            if (A[i] <= A[i - 1]) {
                result += A[i - 1] + 1 - A[i];
                A[i] = A[i - 1] + 1;
            }
        }
        return result;
    }
}
